# -*- coding:utf8 -*-
import json
from datetime import date
from datetime import datetime


class JsonExtendEncoder(json.JSONEncoder):
    """
        This class provide an extension to json serialization for datetime/date.
    """

    def default(self, o):
        """
            provide a interface for datetime/date
        """
        if isinstance(o, datetime):
            return o.strftime('%Y-%m-%d %H:%M:%S')
        elif isinstance(o, date):
            return o.strftime('%Y-%m-%d')
        else:
            return json.JSONEncoder.default(self, o)


def result(success=1, code=0, msg="", data=[]):
    # success = 1 成功 success = 0 失败
    # code = 0 正常 其它为错误代码
    # msg 返回的消息
    # data 返回的数据
    rdict = {}
    rdict["success"] = success
    rdict["code"] = code
    rdict["msg"] = msg
    rdict["data"] = data
    return json.dumps(rdict, cls=JsonExtendEncoder, ensure_ascii=False)
